package com.wangsd.common.component.extBean;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.util.SignUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 分账查询（请求）
 */
@XStreamAlias("xml")
public class PaymentQueryRequest implements Serializable {

    @XStreamAlias("mch_id")
    private String mchId;
    @XStreamAlias("sub_mch_id")
    private String subMchId;
    @XStreamAlias("transaction_id")
    private String transactionId;
    @XStreamAlias("out_order_no")
    private String outOrderNo;
    @XStreamAlias("nonce_str")
    private String nonceStr;
    @XStreamAlias("sign")
    private String sign;
    @XStreamAlias("sign_type")
    private String signType;

    public String toXML() {
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(this.getClass());
        return xstream.toXML(this);
    }

    public void checkAndSign(WxPayConfig config) throws WxPayException {
        if (StringUtils.isBlank(this.getMchId())) {
            this.setMchId(config.getMchId());
        }
        if (StringUtils.isBlank(this.getSubMchId())) {
            this.setSubMchId(config.getSubMchId());
        }
        if (StringUtils.isBlank(this.getSignType())) {
            if (config.getSignType() != null && !WxPayConstants.SignType.ALL_SIGN_TYPES.contains(config.getSignType())) {
                throw new WxPayException("非法的signType配置：" + config.getSignType() + "，请检查配置！");
            }
            this.setSignType(StringUtils.trimToNull(config.getSignType()));
        } else if (!WxPayConstants.SignType.ALL_SIGN_TYPES.contains(this.getSignType())) {
            throw new WxPayException("非法的sign_type参数：" + this.getSignType());
        }

        if (StringUtils.isBlank(this.getNonceStr())) {
            this.setNonceStr(String.valueOf(System.currentTimeMillis()));
        }

        this.setSign(SignUtils.createSign(this, this.getSignType(), config.getMchKey(), this.getIgnoredParamsForSign()));
    }

    protected String[] getIgnoredParamsForSign() {
        return new String[0];
    }


    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getSubMchId() {
        return subMchId;
    }

    public void setSubMchId(String subMchId) {
        this.subMchId = subMchId;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getOutOrderNo() {
        return outOrderNo;
    }

    public void setOutOrderNo(String outOrderNo) {
        this.outOrderNo = outOrderNo;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public void setNonceStr(String nonceStr) {
        this.nonceStr = nonceStr;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSignType() {
        return signType;
    }

    public void setSignType(String signType) {
        this.signType = signType;
    }
}
